package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.BusinessDTO;
import com.api.shopapi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

//统计

@RestController
@RequestMapping("Total")
public class TotalController extends BaseController{

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("BusinessServiceImplOne")
    private BusinessService businessService;

    @Autowired
    @Qualifier("TotalOrderDayServiceImplOne")
    private TotalOrderDayService totalOrderDayService;

    @Autowired
    @Qualifier("TotalOrderMonthServiceImplOne")
    private TotalOrderMonthService totalOrderMonthService;


    //政府管理员获取面板数据
    @RequestMapping(value = "Pannel", method = RequestMethod.POST)
    public MyResult Pannel(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");


            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/Total/Pannel");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);
            String governmentCode = staff.getGovernmentCode();




            int month = new Date().getMonth();
            List<TotalOrderDay> listTotalOrderDayAll = new ArrayList<>();
            List<BusinessDTO> listBusiness = businessService.getListBusinessDTOByGovernmentCode(governmentCode);
            for(BusinessDTO item:listBusiness){
                List<TotalOrderDay> listTotalOrderDay = totalOrderDayService.ListThisMonth(item.getBusinessCode(),month+"");
                for(TotalOrderDay totalOrderDay:listTotalOrderDay){
                    listTotalOrderDayAll.add(totalOrderDay);
                }
            }

            List<TotalOrderDay> listTempDay = new ArrayList<>();
            for(TotalOrderDay item:listTotalOrderDayAll){
                if(hasThisDay(listTempDay,item.getDay())){
                    TotalOrderDay totalOrderDay = getThisDayItem(listTempDay,item.getDay());
                    totalOrderDay.setOrderCount(item.getOrderCount()+totalOrderDay.getOrderCount());
                    totalOrderDay.setOrderMoney(item.getOrderMoney()+totalOrderDay.getOrderMoney());
                }
                else{
                    listTempDay.add(item);
                }
            }



            int year = new Date().getYear();
            List<TotalOrderMonth> listTotalOrderMonthAll = new ArrayList<>();
            for(BusinessDTO item:listBusiness){
                List<TotalOrderMonth> listTotalOrderMonth = totalOrderMonthService.ListThisYear(item.getBusinessCode(),year+"");
                for(TotalOrderMonth totalOrderMonth:listTotalOrderMonth){
                    listTotalOrderMonthAll.add(totalOrderMonth);
                }
            }

            List<TotalOrderMonth> listTempMonth = new ArrayList<>();
            for(TotalOrderMonth item:listTotalOrderMonthAll){
                if(hasThisMonth(listTempMonth,item.getMonth())){
                    TotalOrderMonth totalOrderMonth = getThisMonthItem(listTempMonth,item.getMonth());
                    totalOrderMonth.setOrderCount(item.getOrderCount()+totalOrderMonth.getOrderCount());
                    totalOrderMonth.setOrderMoney(item.getOrderMoney()+totalOrderMonth.getOrderMoney());
                }
                else{
                    listTempMonth.add(item);
                }
            }



            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("listTotalOrderDay",listTempDay);//天订单统计
            jsonObject1.put("listTotalOrderMonth",listTempMonth);//月订单统计

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Total/Pannel", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }






    boolean hasThisDay(List<TotalOrderDay> list,String day){
        for(TotalOrderDay totalOrderDay:list){
            if(totalOrderDay.getDay().equals(day)){
                return true;
            }
        }
        return false;
    }

    TotalOrderDay getThisDayItem(List<TotalOrderDay> list,String day){
        for(TotalOrderDay totalOrderDay:list){
            if(totalOrderDay.getDay().equals(day)){
                return totalOrderDay;
            }
        }
        return null;
    }




    boolean hasThisMonth(List<TotalOrderMonth> list,String month){
        for(TotalOrderMonth totalOrderMonth:list){
            if(totalOrderMonth.getMonth().equals(month)){
                return true;
            }
        }
        return false;
    }

    TotalOrderMonth getThisMonthItem(List<TotalOrderMonth> list,String month){
        for(TotalOrderMonth totalOrderMonth:list){
            if(totalOrderMonth.getMonth().equals(month)){
                return totalOrderMonth;
            }
        }
        return null;
    }





}
